package view.manager;

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import dao.ClassinformationDao;
import dao.CourseDao;
import dao.CourseSelectDao;
import dao.MajorDao;
import model.Classinformation;
import model.Course;
import model.CourseSelect;
import model.Major;
import model.Student;

import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

public class StudentSelectionCourseInterfrm extends JInternalFrame {
	private JTable table;
	private JTable table_1;
	private JComboBox comboBox;
	private JComboBox comboBox_1;
	private boolean optional = false;
	private boolean selected = false;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					StudentSelectionCourseInterfrm frame = new StudentSelectionCourseInterfrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public StudentSelectionCourseInterfrm() {
		setClosable(true);
		setIconifiable(true);
		setTitle("学生选课管理");
		setBounds(100, 100, 642, 583);
		getContentPane().setLayout(null);

		JLabel lblNewLabel = new JLabel("选择班级：");
		lblNewLabel.setBounds(361, 49, 70, 24);
		getContentPane().add(lblNewLabel);

		comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (comboBox.getSelectedItem() instanceof Classinformation) {
					filltableselect(CourseSelectDao.select(), (Classinformation) comboBox.getSelectedItem());
				}
			}
		});
		comboBox.setBounds(430, 50, 122, 23);
		getContentPane().add(comboBox);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setToolTipText("");
		scrollPane.setBounds(82, 338, 470, 172);
		getContentPane().add(scrollPane);

		table = new JTable();
		table.setModel(new DefaultTableModel(new Object[][] { { Boolean.TRUE, null, null, null }, },
				new String[] { "", "\u8BFE\u7A0B\u53F7", "\u8BFE\u7A0B\u540D", "\u5B66\u5206" }) {
			Class[] columnTypes = new Class[] { Boolean.class, Object.class, Object.class, Object.class };

			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}

			boolean[] columnEditables = new boolean[] { true, false, false, false };

			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(0).setPreferredWidth(66);
		scrollPane.setViewportView(table);

		JButton btnNewButton = new JButton("修改");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 过滤掉重复的课程
				for (int i = 0; i < table.getRowCount(); i++) {
					//获取课程号
					if ((boolean)table.getValueAt(i, 0)) {
						if (comboBox.getSelectedItem() instanceof Classinformation) {
							Classinformation classinformation = (Classinformation)comboBox.getSelectedItem();
							//
							if (CourseSelectDao.find(table.getValueAt(i, 1).toString(), classinformation.getCla_id()) == -1) {
								CourseSelectDao.add(classinformation.getCla_id(), table.getValueAt(i, 1).toString());
							}
						}
					}
				}
				JOptionPane.showMessageDialog(null, "修改成功", "提示", JOptionPane.INFORMATION_MESSAGE);
				if (comboBox.getSelectedItem() instanceof Classinformation) {
					filltableselect(CourseSelectDao.select(),(Classinformation)comboBox.getSelectedItem());
					return;
				}
				filltableselect(CourseSelectDao.select());
			}
		});
		btnNewButton.setIcon(new ImageIcon(StudentSelectionCourseInterfrm.class.getResource("/images/updata.png")));
		btnNewButton.setBounds(162, 520, 129, 23);
		getContentPane().add(btnNewButton);

		JButton btnNewButton_1 = new JButton("全选");

		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (optional) {
					btnNewButton_1.setText("全选");
					optional = false;
					filltable(CourseDao.select());
				} else {
					btnNewButton_1.setText("取消全选");
					optional = true;
					filltable(CourseDao.select());
				}
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(StudentSelectionCourseInterfrm.class.getResource("/images/quanxuan.png")));
		btnNewButton_1.setBounds(340, 520, 129, 23);
		getContentPane().add(btnNewButton_1);

		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(82, 111, 470, 161);
		getContentPane().add(scrollPane_1);

		table_1 = new JTable();
		table_1.setModel(new DefaultTableModel(
			new Object[][] {
				{Boolean.TRUE, null, null, null},
			},
			new String[] {
				"", "\u8BFE\u7A0B\u53F7", "\u8BFE\u7A0B\u540D", "\u5B66\u5206"
			}
		) {
			Class[] columnTypes = new Class[] {
				Boolean.class, Object.class, Object.class, Object.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
			boolean[] columnEditables = new boolean[] {
				true, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		scrollPane_1.setViewportView(table_1);

		JLabel lblNewLabel_1 = new JLabel("已选课程：");
		lblNewLabel_1.setBounds(82, 83, 93, 15);
		getContentPane().add(lblNewLabel_1);

		JLabel lblNewLabel_2 = new JLabel("可选课程：");
		lblNewLabel_2.setBounds(82, 313, 96, 15);
		getContentPane().add(lblNewLabel_2);

		JLabel lblNewLabel_3 = new JLabel("请选择专业：");
		lblNewLabel_3.setBounds(82, 54, 75, 15);
		getContentPane().add(lblNewLabel_3);

		comboBox_1 = new JComboBox();
		comboBox_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				comboBox.removeAllItems();
				if (comboBox_1.getSelectedItem() instanceof Major) {
					fillClassType((Major) comboBox_1.getSelectedItem());
				}
			}
		});
		comboBox_1.setBounds(150, 50, 201, 23);
		getContentPane().add(comboBox_1);
		
		JButton btnNewButton_2 = new JButton("删除");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				for (int i = 0; i < table_1.getRowCount(); i++) {
					//获取课程号
					if ((boolean)table_1.getValueAt(i, 0)) {
						if (comboBox.getSelectedItem() instanceof Classinformation) {
							Classinformation classinformation = (Classinformation)comboBox.getSelectedItem();
							CourseSelect courseSelect = new CourseSelect(table_1.getValueAt(i, 1).toString(), classinformation.getCla_id());
							CourseSelectDao.delete(courseSelect);
						}
					}
				}
				JOptionPane.showMessageDialog(null, "修改成功", "提示", JOptionPane.INFORMATION_MESSAGE);
				if (comboBox.getSelectedItem() instanceof Classinformation) {
					filltableselect(CourseSelectDao.select(), (Classinformation) comboBox.getSelectedItem());
				}
			}
		});
		btnNewButton_2.setIcon(new ImageIcon(StudentSelectionCourseInterfrm.class.getResource("/images/删 除 .png")));
		btnNewButton_2.setBounds(162, 280, 129, 23);
		getContentPane().add(btnNewButton_2);
		
		JButton btnNewButton_3 = new JButton("全选");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (selected) {
					btnNewButton_3.setText("全选");
					selected = false;
					if (comboBox.getSelectedItem() instanceof Classinformation) {
						filltableselect(CourseSelectDao.select(), (Classinformation) comboBox.getSelectedItem());
					}
				} else {
					btnNewButton_3.setText("取消全选");
					selected = true;
					if (comboBox.getSelectedItem() instanceof Classinformation) {
						filltableselect(CourseSelectDao.select(), (Classinformation) comboBox.getSelectedItem());
					}
				}
			}
		});
		btnNewButton_3.setIcon(new ImageIcon(StudentSelectionCourseInterfrm.class.getResource("/images/quanxuan.png")));
		btnNewButton_3.setBounds(340, 280, 129, 23);
		getContentPane().add(btnNewButton_3);
		filltable(CourseDao.select());

		fillMajorType();
	}

	/**
	 * 表格填充
	 * 
	 * @param arrayList
	 */
	private void filltable(ArrayList<Course> arrayList) {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		dtm.setRowCount(0);
		for (Course course : arrayList) {
			if ("必修".equals(course.getCou_category())) {
				Vector v = new Vector();
				v.add(optional);
				v.add(course.getCou_id());
				v.add(course.getCou_name());
				v.add(course.getCou_credit());
				dtm.addRow(v);
			}
		}
	}

	private void filltableselect(ArrayList<CourseSelect> arrayListCourseSelect) {
		DefaultTableModel dtm = (DefaultTableModel) table_1.getModel();
		dtm.setRowCount(0);
		for (CourseSelect course : arrayListCourseSelect) {
			Vector v = new Vector();
			v.add(selected);
			v.add(course.getCou_id());
			v.add(course.getCou_name());
			v.add(course.getCou_credit());
			dtm.addRow(v);
		}
	}

	private void filltableselect(ArrayList<CourseSelect> arrayListCourseSelect, Classinformation classinformation) {
		DefaultTableModel dtm = (DefaultTableModel) table_1.getModel();
		dtm.setRowCount(0);
		for (CourseSelect course : arrayListCourseSelect) {
			if (course.getCla_id().equals(classinformation.getCla_id())) {
				Vector v = new Vector();
				v.add(selected);
				v.add(course.getCou_id());
				v.add(course.getCou_name());
				v.add(course.getCou_credit());
				dtm.addRow(v);
			}
		}
	}

	/**
	 * 初始化专业下拉框
	 */
	private void fillMajorType() {
		comboBox_1.addItem("请选择专业");
		ArrayList<Major> arrayList = MajorDao.select();
		for (Major major : arrayList) {
			comboBox_1.addItem(major);
		}
	}

	/**
	 * 初始化下拉框
	 */

	private void fillClassType() {
		comboBox.addItem("请选择班级");
		ArrayList<Classinformation> arrayList = ClassinformationDao.select();
		for (Classinformation classinformation : arrayList) {
			comboBox.addItem(classinformation);
		}
	}

	/**
	 * 择班级初始化下拉框
	 */
	private void fillClassType(Major major) {
		comboBox.addItem("请选择班级");
		if (comboBox_1.getSelectedItem() instanceof Major) {
			ArrayList<Classinformation> arrayList = ClassinformationDao.select();
			for (Classinformation classinformation : arrayList) {
				if (classinformation.getM_id().equals(major.getM_id())) {
					comboBox.addItem(classinformation);
				}
			}
		}
	}
	
}
